package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        //获取菜品id或套餐id或菜品口味
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //获取当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //1.判断加入购物车中的商品是否已存在,
        //注意这里根据userId、dishId或setmealId或dishFlavor查询结果只能为 null 或 一个shoppingCart对象
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);


        if(list != null && list.size() > 0){
            //2.商品已在购物车中 -> 数量+1
            ShoppingCart car = list.get(0);
            car.setNumber(car.getNumber() + 1);
            //更新数据库的shopping_cart表
            shoppingCartMapper.updateNumberById(car);
        }else {
            //3.商品不在购物车中 -> 插入购物车数据


            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //3.1 添加的商品是 dish菜品
                //3.2 根据dishId查询并封装成dish对象
                Dish dish = dishMapper.getById(dishId);

                //3.3 插入的字段有 dish_name dish_image dish_number dish_price caret_time
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //4.1 添加的商品是 setmeal套餐
                Long setmealId = shoppingCartDTO.getSetmealId();

                //4.2 根据setmealId查询并封装成setmeal对象
                Setmeal setmeal = setmealMapper.getById(setmealId);

                //4.3 插入的字段有 setmeal_name setmeal_image setmeal_number setmeal_price caret_time
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            //不管是菜品还是套餐，第一次添加到购物车的商品数量都为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //5.将封装好的ShoppingCart对象插入shopping_cart表
            shoppingCartMapper.insert(shoppingCart);

        }

    }


    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前用户id
        Long currentUserId = BaseContext.getCurrentId();

        //根据当前用户id查询其购物车
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentUserId)
                .build();


        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }


    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取当前用户id
        Long currentUserId = BaseContext.getCurrentId();

        //根据当前用户id删除购物车的全部商品
        shoppingCartMapper.deleteByUserId(currentUserId);
    }


    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();

        //1.获取菜品id或套餐id
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //2.获取当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //3.根据当前用户id和要删除的菜品id或套餐id去查询购物车
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);


        if(list != null && list.size() > 0){
            //获取当前用户要删除的商品，因为每次请求只删除一个商品，所以只有一个
            ShoppingCart car = list.get(0);


            Integer number = car.getNumber();
            if(number == 1){
                //4.删除的商品在购物车的数量为1，则直接在购物车刪除该商品
                shoppingCartMapper.deleteById(car.getId());
            }else {
                //5.删除的商品在购物车的数量大于1，则直接将该商品在购物车的数量减一
                car.setNumber(car.getNumber() - 1);
                shoppingCartMapper.updateNumberById(car);
            }
        }

    }

}
